*** Settings ***
Documentation       搭建redfish工具所需要的命令和变量
Resource            ../resource.robot
Library             Collections
Library             RequestsLibrary

*** Keywords ***
Create REDFISH_POST Session
    [Documentation]    创建redfish post登录方法
    Create Session    redfish_post    ${REDFISH_BASE}    verify=${False} 

Create REDFISH_GET Session
    [Documentation]    创建redfish get登录方法

    # 创建认证元组变量
    ${AUTH_CREDENTIALS}    Create List    ${OPENUBMC_USERNAME}    ${OPENUBMC_PASSWORD}
    Create Session    redfish_get    ${REDFISH_BASE}    auth=${AUTH_CREDENTIALS}    verify=${False}

REDFISH_POST Request
    [Documentation]    发送redfish post登录方法请求并返回响应体
    [Arguments]    ${url}    ${body}=${None}    ${expected_status}=200    ${headers}=${None}

    # 默认请求头
    ${default_headers}=    Create Dictionary    Content-Type=application/json
    ${request_headers}=    Set Variable If    ${headers} is not None    ${headers}    ${default_headers}
    
    # 发送POST请求
    ${rsp}=    POST    ${REDFISH_BASE}${url}
    ...    headers=${request_headers}
    ...    json=${body}
    ...    expected_status=${expected_status}
    ...    verify=${False}

    RETURN    ${rsp}

Create Redfish_PATCH Session
    [Documentation]    创建redfish patch 登录方法
    # 创建临时会话获取Token
    Create Session    temp_session    ${REDFISH_BASE}
    ...    verify=${SSL_VERIFY}
    ...    disable_warnings=1

    # 获取认证token
    ${auth}=    Create Dictionary
    ...    UserName=${OPENUBMC_USERNAME}
    ...    Password=${OPENUBMC_PASSWORD}

    ${resp}=    POST On Session    temp_session
    ...    ${REDFISH_SESSION}
    ...    json=${auth}

    ${token}=    Get From Dictionary    ${resp.headers}    X-Auth-Token
    Set Suite Variable    ${AUTH_TOKEN}    ${token}

    # 创建正式会话并携带Token
    ${headers}=    Create Dictionary
    ...    X-Auth-Token=${AUTH_TOKEN}
    ...    Content-Type=application/json

    Create Session    redfish_patch    ${REDFISH_BASE}
    ...    headers=${headers}
    ...    verify=${SSL_VERIFY}
    ...    disable_warnings=1

Get User ETag
    [Documentation]    获取Etag
    ${headers}=    Create Dictionary
    ...    X-Auth-Token=${AUTH_TOKEN}
    ...    Content-Type=application/json

    ${response}=    GET On Session    redfish_patch
    ...    ${INPUT_URI}
    ...    headers=${headers}

    ${etag}=    Get From Dictionary    ${response.headers}    ETag
    ${etag}=    Set Variable If
    ...    $etag is None    "*"
    ...    ${etag}
    Set Suite Variable    ${ETAG}    ${etag}
    Log    ETag值为: ${ETAG}